//
//  EmitView.swift
//  FirstSwiftProduct
//
//  Created by 知合金服-Mini on 2017/12/4.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

class EmitView: UIView {
    
    var emitter : CAEmitterLayer!
    
    public var _cellArr:NSMutableArray?
    var cellArr:NSMutableArray{
        get{
            return _cellArr!
        }
        
        set{
            _cellArr?.removeAllObjects()
            _cellArr = newValue
            for i in 0...cellArr.count {
                
                let cell = CAEmitterCell()//发射单元
                cell.birthRate = 1 ////粒子速率 默认1/s  粒子的创建速率
                cell.lifetime = Float(arc4random_uniform(4) + 1) //粒子存活时间
                cell.lifetimeRange = 1.5 //粒子生存时间容差
                let cellImage = UIImage.init(named: NSString.init(format: "good%d_30x30_", i) as String)
                cell.contents = cellImage?.cgImage
                //粒子运动速度  改变此属性改变例子速度
                cell.velocity = CGFloat(arc4random_uniform(100) + 100)
                cell.velocityRange = 80; //粒子运动速度容差
                //粒子在xy平面的发射角度
                cell.emissionLongitude = 4.6;
                cell.emissionRange = 0.3; //发射角度容差
                cell.scale = 0.3; //缩放比例
                cellArr.add(cell)
            }
            emitter.emitterCells = (cellArr as! [CAEmitterCell])
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        emitter = CAEmitterLayer()
        //发射器在平面上的中心位置
        emitter.emitterPosition = CGPoint(x: frame.size.width - 50, y: frame.size.height - 50)
        //发射器的尺寸
        emitter.emitterSize = CGSize(width: 20, height: 20)
        //渲染模式
        emitter.renderMode = kCAEmitterLayerUnordered
        
        self.layer.addSublayer(emitter)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
    
}
